package com.woyou.ui.component;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.PopupWindow.OnDismissListener;
import android.widget.TextView;

import com.citaq.ideliver.R;
import com.woyou.bean.GoodsType;
import com.woyou.ui.activity.ShopInfoActivity.FragmentControllerListener;
import com.woyou.ui.adapter.GoodsTypeAdapter;

/**
 * 菜品列表的pop窗口
 * 
 * @author shenzhou 2014年12月26日
 */
public class DishesTypePop {
	
	private static Context mContext;
	private static View positionView;
	private static View mShade;
	private static ImageView buttomTrigon;
	private static FragmentControllerListener mListener;
	
	private PopupWindow popupWindow;
	private ListView mListView;
	private static DishesTypePop instance = null;
	private GoodsTypeAdapter goodsTypeAdapter;
	ListItemClick mListItemClick;
	GoodsType removeItem;
	List<GoodsType> mList = new ArrayList<GoodsType>();
	List<GoodsType> goodsTypeList = new ArrayList<GoodsType>();
	View view = null;
	
	int w = 0;
	int h = 0;
	private DishesTypePop() {
		initView();
	}
	
	private void initView() {
		GoodsType type = new GoodsType();
		type.setNum("");
		type.settId("");
		type.settName("全部商品");
		goodsTypeList.add(0, type);	
		
		view = LayoutInflater.from(mContext).inflate(R.layout.pop_dishes, null);
		mListView = (ListView) view.findViewById(R.id.dishespop_list);
		
		DisplayMetrics dm = new DisplayMetrics();
		((Activity) mContext).getWindowManager().getDefaultDisplay().getMetrics(dm);
		int widthPixels = dm.widthPixels;
		w = widthPixels/2;
		h = ViewGroup.LayoutParams.WRAP_CONTENT;
		
		goodsTypeAdapter = new GoodsTypeAdapter(mContext, mList);
		mListView.setAdapter(goodsTypeAdapter);
		
		mListView.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				removeItem = mList.get(position);
				mListItemClick.ItemClick(removeItem);
				refreshList();
				TextView text = (TextView) positionView.findViewById(R.id.dishes_type);
				text.setText(removeItem.gettName());
				popupWindow.dismiss();
			}
		});
	}

	public static DishesTypePop getInstance (Context context, View view, View bgShade, ImageView imageView, FragmentControllerListener l) {
		mContext = context;
		positionView = view;
		mShade = bgShade;
		buttomTrigon = imageView;
		mListener = l;
		
		if( instance == null ){
			instance = new DishesTypePop();
		}
		return instance;
	}
	
	public void init(List<GoodsType> goodsList) {
		
		if ( goodsTypeList.size()== 1){
			goodsTypeList.addAll(goodsList);
			// 实例化泡泡窗台的布局文件
			if ( goodsTypeList.size() > 8 ) {
				h = (int) mContext.getResources().getDimension(R.dimen.dp1100);
			}else{
				h = ViewGroup.LayoutParams.WRAP_CONTENT;
			}
			// 实例化泡泡窗体
			popupWindow = new PopupWindow(view, w, h);
			popupWindow.setFocusable(true);
			popupWindow.setBackgroundDrawable(new ColorDrawable(00000000));// 背景透明
			popupWindow.setOnDismissListener(new OnDismissListener() {
				@Override
				public void onDismiss() {
					buttomTrigon.setVisibility(View.VISIBLE);
					mShade.setVisibility(View.GONE);
					mListener.setPopFlag(false);
				}
			});
			
		}
		refreshList();
		
	}
	
	private void refreshList(){
		if( removeItem == null ){
			removeItem = goodsTypeList.get(0);
		}
		
		mList.clear();
		mList.addAll(goodsTypeList);
		mList.remove(removeItem);
		
		if ( mList.size() == 0 )
			return;
		goodsTypeAdapter.notifyDataSetChanged();
	}
	
	/**
	 * 弹出泡泡窗体
	 */
	public void showPopup() {
		if( mListView!=null )
			mListView.setSelection(0);  //每次显示列表置顶
		
		mShade.setVisibility(View.VISIBLE);
		int h = (int) mContext.getResources().getDimension(R.dimen.dp2);
		popupWindow.showAsDropDown(positionView, 0, h);  //修改位置
	}
	
	/**
	 * 隐藏pop窗体
	 */
	public void hidePopup() {
		if (popupWindow != null && popupWindow.isShowing()) {
			popupWindow.dismiss();
			mShade.setVisibility(View.GONE);
		}
	}
	
	public interface ListItemClick {
		void ItemClick(GoodsType goodsType);
	}

	public void setmListItemClick(ListItemClick mListItemClick) {
		this.mListItemClick = mListItemClick;
	}
	
	public void clear(){
		mList.clear();
		removeItem = null;
		goodsTypeList.clear();
		GoodsType type = new GoodsType();
		type.setNum("");
		type.settId("");
		type.settName("全部商品");
		goodsTypeList.add(0, type);		
	}
	
}
